<template>
  <div>
    <!-- 列表页面 -->
    <div class="container" v-if="!showEdit">
      <div class="header">
        <div class="title">合同列表</div>
        <div>
          <el-button @click="handleAdd">新增合同</el-button>
        </div>
      </div>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="contractname" label="合同名称" width="180">
        </el-table-column>
        <el-table-column prop="status" label="合同状态" width="180">
          <template slot-scope="scope">
            <span v-if="scope.row.status == '0'">未生成合同</span>
            <span v-if="scope.row.status == '1'">待签署</span>
            <span v-if="scope.row.status == '2'">已签署</span>
          </template>
        </el-table-column>
        <el-table-column prop="pdf" label="pdf地址">
          <template slot-scope="scope">
            <span v-if="scope.row.pdf == '0'">暂无</span>
            <span v-else>{{ scope.row.pdf }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="400">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status == 0"
              @click="handleData(scope.row)"
              type="text"
              size="small"
              >生成未签字合同</el-button
            >
            <el-button
              v-if="scope.row.status == 2"
              @click="handleClick(scope.row)"
              type="text"
              size="small"
              >生成最终合同</el-button
            >
            <el-button @click="handleView(scope.row)" type="text" size="small"
              >查看</el-button
            >
            <el-button @click="handleEdit(scope.row)" type="text" size="small"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 编辑页面 -->
    <book-modify v-else @editClose="editClose" :editBookID="editBookID"></book-modify>
  </div>
</template>

<script>
import contract from "@/model/contract";
import LinTable from "@/component/base/table/lin-table";
import BookModify from "./book-modify";
import ParseTime from "@/lin/util/parse-time";

export default {
  components: {
    LinTable,
    BookModify,
  },
  data() {
    return {
      tableColumn: [
        { prop: "title", label: "书名" },
        { prop: "author", label: "作者" },
      ],
      tableData: [],
      operate: [],
      showEdit: false,
      editBookID: 1,
    };
  },
  async created() {
    this.loading = true;
    await this.getList();
    this.loading = false;
  },
  methods: {
    async getList() {
      try {
        const obj = {
          Opcode: "1113",
          page: "1",
          pagesize: "50",
          data: {},
        };
        const data = await contract.contractinfolist(obj);
        this.tableData = data.data.data;
      } catch (error) {
        if (error.code === 10020) {
          this.tableData = [];
        }
      }
    },
    handleData(val) {
      this.$router.push({
        path: "/contract/add",
        query: {
          contractinfoid: val._id,
          contractid: val.contractid,
          type: "first",
        },
      });
    },
    handleClick() {
      this.$router.push({
        path: "/contract/add",
        query: {
          contractinfoid: val._id,
          contractid: val.contractid,
          type: "end",
        },
      });
    },
    handleAdd() {
      this.showEdit = true;
      // this.$router.push({
      //   path: "/contract/add",
      // });
    },
    handleView(val) {
      if (val.pdf == 0) {
        this.$message({
          message: "还未生成pdf",
          type: "error",
        });
      } else {
        this.$router.push({
          path: "/contract/add",
          query: {
            contractid: val.contractid,
            type: "view",
          },
        });
      }
    },
    handleEdit(val) {
      this.$router.push({
        path: "/contract/add",
        query: {
          contractid: val.contractid,
          contractinfoid: val._id,
          type: "edit",
        },
      });

      // this.showEdit = true;
      // this.editBookID = val.row.id;
    },
    handleDelete(val) {
      this.$confirm("此操作将永久删除该图书, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const res = await book.deleteBook(val.row.id);
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.getList();
          this.$message({
            type: "success",
            message: `${res.message}`,
          });
        }
      });
    },
    rowClick() {},
    editClose() {
      this.showEdit = false;
      this.getList();
    },
    // 导出表格
    exprotExcel() {
      // 动态导入
      import("@/lin/util/export-excel").then((excel) => {
        const tHeader = ["timestamp", "title", "label", "importance", "status"];
        const filterVal = ["timestamp", "title", "label", "importance", "status"];
        const data = this.formatJson(filterVal);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: "table-list",
        });
      });
    },
    // 将表单格式化为json数据
    formatJson(filterVal) {
      return this.tableData.map((v) =>
        filterVal.map((j) => {
          if (j === "timestamp") {
            return ParseTime(v[j]);
          }
          return v[j];
        })
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 0 30px;
  min-height: 800px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    margin: 20px;
  }
}
</style>
